﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using RealtorsPortal.Entities;
using System.Data.SqlClient;

namespace RealtorsPortal.Data_Access_Layer
{
    public class AdvCommentDAL
    {
        DataAccessHelper dah = new DataAccessHelper();

        public DataTable GetCommentsByAdvIDDataTable(string advId)
        {
            string cmd = @"select CommentId, FullName, Date, Content from AdsComment where AdvId ="+ advId+"  order by CommentId DESC";
            return dah.GetDataTable(cmd);
        }

        public List<AdvComment> GetCommentsByAdvIDList(string advId)
        {
            DataTable dt = this.GetCommentsByAdvIDDataTable(advId);
            List<AdvComment> list = new List<AdvComment>();
            foreach(DataRow r in dt.Rows){
                AdvComment c = new AdvComment();
                c.CommentId = (int)r["CommentId"];
                c.Content = r["Content"].ToString();
                c.FullName = r["FullName"].ToString();
                c.WritedDate = r["Date"].ToString();
                list.Add(c);
            }
            return list;
        }

        public bool InsertComment(AdvComment c) {
            List<SqlParameter> list = new List<SqlParameter>();
            list.Add(new SqlParameter("@advId", c.AdvId));
            list.Add(new SqlParameter("@content", c.Content));
            list.Add(new SqlParameter("@date", c.WritedDate));
            list.Add(new SqlParameter("@fullname", c.FullName));
            return dah.ExcuteStoredNonQuery("sp_insertComment", list) > 0;
        }
    }
}